package com.manager.kudian.system.utils;


import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.Charset;

/**
 * Created by Administrator on 2016/4/21.
 */
public class HttpUtils {

    // 接口地址
    private HttpClient httpClient = null;
    private HttpPost method = null;
    private int status = 0;

    private String apiURL;

    public HttpUtils(String url) {

        if (url != null) {
            this.apiURL = url;
        }
        if (apiURL != null) {
            httpClient = new DefaultHttpClient();
            method = new HttpPost(apiURL);
        }
    }

    /**
     * 调用 API
     *
     * @param json
     * @return
     */
    public String post(String json) {
        String body = null;

        if (method != null & json != null
                && !"".equals(json.trim())) {
            try {
                // 建立一个NameValuePair数组，用于存储欲传送的参数
                method.addHeader("Content-type","application/json; charset=utf-8");
                method.setHeader("Accept", "application/json");
                //解决中文乱码问题
                method.setEntity(new StringEntity(json, Charset.forName("UTF-8")));
                HttpResponse response = httpClient.execute(method);
                int statusCode = response.getStatusLine().getStatusCode();
                if (statusCode != HttpStatus.SC_OK) {
                    status = 1;
                }
                // Read the response body
                body = EntityUtils.toString(response.getEntity());
            } catch (IOException e) {
                // 网络错误
                status = 2;
            }
        }
        return body;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public static void main(String[] args) {


//        JsonArray arry = new JsonArray();
//        JsonObject j = new JsonObject();
//        j.addProperty("orderId", "中文");
//        j.addProperty("createTimeOrder", "2015-08-11");
//        arry.add(j);
//        System.out.println(ac.post(arry.toString()));

        // 我的工程里没有jsonArray
//        // TODO 这个地方主要用JsonObject变换为json串传到服务器端
//        String json = "{\n" +
//                "  \"name\": \"guoling\",\n" +
//                "  \"password\": \"pwd\"\n" +
//                "}";
//
//        HttpUtils u = new HttpUtils("http://101.201.74.2/user/login");
//        System.out.println(u.post(json));
//        // 可以取得状态 1:为正常  2:为异常
//        System.out.println(u.getStatus());

        String fileName = "图书txt";
        int offset = fileName.lastIndexOf(".");
        if (offset < 0) {

        }
        System.out.println(offset);

        System.out.println(fileName.substring(fileName.lastIndexOf(".")+1));
    }
}
